﻿using B.S.DataBase.MyProject.Domain.RBAC;

using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Users;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Users
{
    public class QueryUserCommandHandler:IRequestHandler<QueryUserCommand,APIResult<List<User>>>
    {
        private readonly IBaseRepository<User> uRepo;

        public QueryUserCommandHandler(IBaseRepository<User>uRepo)
        {
            this.uRepo = uRepo;
        }

        public async Task<APIResult<List<User>>> Handle(QueryUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<User>> result = new APIResult<List<User>>();
            try
            {
                var list = uRepo.GetAll().Result;         
                result.data = list.ToList();
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"添加团队信息时发生异常: {ex.Message}");
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
